package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.common.Constants;
import cn.com.xf.dao.TbempinfoDAO;
import cn.com.xf.dao.TblCheckDeviceDAO;
import cn.com.xf.dao.TsDictDAO;
import cn.com.xf.dao.TsSiteinfoDAO;
import cn.com.xf.dao.TsUserDAO;
import cn.com.xf.domain.Tbempinfo;
import cn.com.xf.domain.TblCheckDevice;
import cn.com.xf.domain.TsDict;
import cn.com.xf.domain.TsSiteinfo;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private TsSiteinfoDAO tsSiteinfoDAO;
	@Autowired
	private TsDictDAO tsDictDAO;
	@Autowired
	private TsUserDAO tsUserDAO;
	@Autowired
	private TblCheckDeviceDAO deviceDAO;
	@Autowired
	private TbempinfoDAO empDao;

	@Override
	public List<TsSiteinfo> selectDirectSites() {
		return tsSiteinfoDAO.selectDirectSites();
	}

	@Override
	public List<TsDict> selectDepts() {
		return tsDictDAO.selectDepts();
	}

	@Override
	public List<TsUser> selectUsers(String siteId) {
		return tsUserDAO.selectUserBySiteId(siteId);
	}

	@Override
	public Map<String, Object> getDetps() {
		List<TsDict> list = tsDictDAO.selectDepts();
		Map<String, Object> map = new HashMap<String, Object>();
		for (TsDict dict: list) {
			map.put(dict.getDictid(), dict.getDictname());
		}
		return map;
	}

	@Override
	public List<Map<String, Object>> getDeptsAndUsers(String siteid) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		TsSiteinfo siteinfo = tsSiteinfoDAO.selectByPrimaryKey(siteid);
		if (siteinfo == null)
			return list;
		List<TsDict> deptList = tsDictDAO.selectDepts();
		for (TsDict dept: deptList) {
			Map<String, Object> deptMap = new HashMap<String, Object>();
			deptMap.put("id", dept.getDictid());
			deptMap.put("text", dept.getDictname());
//			List<TsUser> userList = tsUserDAO.selectUserBySiteIdAndDeptid(siteid, dept.getDictid());
//			if (userList.size() > 0) {
//				List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
//				for (TsUser user: userList) {
//					Map<String, Object> userMap = new HashMap<String, Object>();
//					userMap.put("id", user.getUserid());
//					userMap.put("text", user.getUsername());
//					children.add(userMap);
//				}
//				deptMap.put("state", "closed");
//				deptMap.put("children", children);
//				list.add(deptMap);
//			}
			List<Tbempinfo> empList = empDao.selectEmpBySiteIdAndDeptid(siteid, dept.getDictid());
			if (empList.size() > 0) {
				List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
				for (Tbempinfo emp: empList) {
					Map<String, Object> userMap = new HashMap<String, Object>();
					userMap.put("id", emp.getEmpid());
					userMap.put("text", emp.getEmpname());
					children.add(userMap);
				}
				deptMap.put("state", "closed");
				deptMap.put("children", children);
				list.add(deptMap);
			}
		}
		List<Tbempinfo> eList = empDao.selectEmpBySiteidWithoutDept(siteid);
		for (Tbempinfo e: eList) {
			Map<String, Object> eMap = new HashMap<String, Object>();
			eMap.put("id", e.getEmpid());
			eMap.put("text", e.getEmpname());
			list.add(eMap);
		}
		Map<String, Object> allMap = new HashMap<String, Object>();
		List<Map<String, Object>> allChildren = new ArrayList<Map<String,Object>>();
		allMap.put("id", siteid);
		allMap.put("text", siteinfo.getSitename());
		allMap.put("state", "closed");
		allMap.put("children", list);
		allChildren.add(allMap);
		return allChildren;
	}
	
	@Override
	public List<Map<String, Object>> datagridDepts(String siteid) {
		List<Tbempinfo> deptList = empDao.selectGetDept(siteid);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Tbempinfo td = new Tbempinfo();
		td.setDeptid("D999");
		td.setDeptname("其它");
		deptList.add(td);
		for (Tbempinfo dept: deptList) {
			Map<String, Object> deptMap = new HashMap<String, Object>();
			deptMap.put("deptid", dept.getDeptid());
			deptMap.put("deptname", dept.getDeptname());
			list.add(deptMap);
		}		
		return list;
	}
	@Override
	public List<Map<String, Object>> datagridUsers(String siteid,String deptid) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		List<Tbempinfo> deptList = empDao.selectGetDept(siteid);
		Tbempinfo td = new Tbempinfo();
		td.setDeptid("D999");
		td.setDeptname("其它");
		deptList.add(td);
		for (Tbempinfo dept: deptList) {			
			if(dept.getDeptid().equals("D999") && deptid.equals("D999")){
				List<Tbempinfo> eList = empDao.selectEmpBySiteidWithoutDept(siteid);
				for (Tbempinfo e: eList) {
					Map<String, Object> eMap = new HashMap<String, Object>();
					eMap.put("userid", e.getEmpid());
					eMap.put("username", e.getEmpname()+"("+e.getEmpid()+")");
					list.add(eMap);
				}
			}else if(dept.getDeptid().equals(deptid)){
				List<Tbempinfo> empList = empDao.selectEmpBySiteIdAndDeptid(siteid, deptid);
				if (empList.size() > 0) {
					for (Tbempinfo emp: empList) {
						Map<String, Object> userMap = new HashMap<String, Object>();
						userMap.put("userid", emp.getEmpid());
						userMap.put("username", emp.getEmpname()+"("+emp.getEmpid()+")");
						list.add(userMap);
					}
				}
			}
		}
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getDevices() {
		List<TblCheckDevice> devices = deviceDAO.selectAll();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (TblCheckDevice device: devices) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", device.getId());
			map.put("text", device.getName());
			list.add(map);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getDeptsAndUsersWithCarline(String siteid) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if (siteid.equals("HUMEN")) {
			for (int i = 0; i < Constants.HUMEN_CARLINE.length; i++) {
				list.addAll(getDeptsAndUsers(Constants.HUMEN_CARLINE[i]));
			}
			Map<String, Object> allMap = new HashMap<String, Object>();
			List<Map<String, Object>> allChildren = new ArrayList<Map<String,Object>>();
			allMap.put("id", siteid);
			allMap.put("text", "虎门仓车线");
			allMap.put("state", "closed");
			allMap.put("children", list);
			allChildren.add(allMap);
			return allChildren;
		} else if (siteid.equals("SHENZHEN")) {
			for (int i = 0; i < Constants.SHENZHEN_CARLINE.length; i++) {
				list.addAll(getDeptsAndUsers(Constants.SHENZHEN_CARLINE[i]));
			}
			Map<String, Object> allMap = new HashMap<String, Object>();
			List<Map<String, Object>> allChildren = new ArrayList<Map<String,Object>>();
			allMap.put("id", siteid);
			allMap.put("text", "深圳仓车线");
			allMap.put("state", "closed");
			allMap.put("children", list);
			allChildren.add(allMap);
			return allChildren;
		} else {
			list.addAll(getDeptsAndUsers(siteid));
			return list;
		}

	}

	@Override
	public List<TsSiteinfo> selectSites() {
		// TODO Auto-generated method stub
		return tsSiteinfoDAO.selectSites();
	}

}
